<?php
Class Message_m extends CI_Model
{

	/********************************************************************
	message functions
	********************************************************************/
	function get_messages()
	{
		$this->db->order_by('sequence', 'ASC');
		$result = $this->db->get('messages')->result();
		$return	= array();
		foreach($result as $message){
			// Set a class to active, so we can highlight our current message
			if($this->uri->segment(1) == $message->slug) {
				$message->active = true;
			} else {
				$message->active = false;
			}
			$return[$message->id]				= $message;
		}
		
		return $return;
	}

	function get_message($id)
	{
		$this->db->where('id', $id);
		$result = $this->db->get('messages')->row();
		return $result;
	}
	
	function get_slug($id)
	{
		$message = $this->get_message($id);
		if($message) 
		{
			return $message->slug;
		}
	}
	
	function save($data){
		if($data['id']){
			$this->db->where('id', $data['id']);
			$this->db->update('messages', $data);
			return $data['id'];
		}else{
			$this->db->insert('messages', $data);
			return $this->db->insert_id();
		}
	}
	
	function delete_message($id){
		//delete the message
		$this->db->where('id', $id);
		$this->db->delete('messages');
	}
	
	function get_message_by_slug($slug){
		$this->db->where('slug', $slug);		
		return 
		$this->db->get('messages')->row();
		echo $this->db->last_query();
	}
}